package com.kykj.kykjyzw.service.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.qiniu.util.Md5;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class TokenToolService {
    @Resource
    private RedisTemplate<String,String> redisTemplate;
    public String createToken(String uname){
      String token=  JWT.create().withAudience(uname).sign(Algorithm.HMAC256(uname));
      redisTemplate.opsForValue().set(uname,token,60*6, TimeUnit.MINUTES);//存入redis
      return token;
    }
    public  boolean isToKen(String uname,String token){
        if (redisTemplate.opsForValue().get(uname).equals(token)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除token (退出登录)
     * @param uname 用户名
     * @return boolean: True:退出成功，Fales:退出失败
     */
    public boolean delToken(String uname){
        return redisTemplate.delete(uname);//删除key
    }
}
